﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace PMCT
{
    public partial class ConfigDimension : Form
    {
        public ConfigRoot curRoot;

        public ConfigDimension()
        {
            InitializeComponent();
        }

        private void ConfigDimension_Load(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();

            foreach (Dimension d in curRoot.listDimension)
            {
                int i = dataGridView1.Rows.Add();
                dataGridView1.Rows[i].Cells[0].Value = d.name;

                dataGridView1.Rows[i].Cells[1].Value = false;
                if (d.isMultiple)
                    dataGridView1.Rows[i].Cells[1].Value = true;
            }
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            dataGridView2.Rows.Clear();

            int index = dataGridView1.SelectedRows[0].Index;
            if (index >= curRoot.listDimension.Count)
                return;

            Dimension d = curRoot.listDimension[index];

            foreach (DimensionItem item in d.list)
            {
                int i = dataGridView2.Rows.Add();
                dataGridView2.Rows[i].Cells[0].Value = item.name;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= dataGridView1.Rows.Count)
                return;

            if (e.ColumnIndex == 1) // 修改isMultiple
            {
                bool v = (bool)dataGridView1[e.ColumnIndex, e.RowIndex].Value;
                if (e.RowIndex >= curRoot.listDimension.Count)
                {
                    Dimension d = new Dimension();
                    d.isMultiple = v;
                    curRoot.listDimension.Add(d);
                }
                else
                    curRoot.listDimension[e.RowIndex].isMultiple = v;
            }
            else if (e.ColumnIndex == 0) // 修改名称
            {
                string v = (string)dataGridView1[e.ColumnIndex, e.RowIndex].Value;
                if (v != null)
                {
                    if (e.RowIndex >= curRoot.listDimension.Count)
                    {
                        Dimension d = new Dimension();
                        d.name = v;
                        curRoot.listDimension.Add(d);
                    }
                    else
                        curRoot.listDimension[e.RowIndex].name = v;
                }
            }
        }

        private void dataGridView2_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            string v = (string)dataGridView2[e.ColumnIndex, e.RowIndex].Value;
            if (v == null)
                return;

            int index = dataGridView1.SelectedRows[0].Index;
            Dimension d = curRoot.listDimension[index];

            if (e.RowIndex >= d.list.Count)
            {
                DimensionItem di = new DimensionItem(v);
                d.list.Add(di);
            }
            else
                d.list[e.RowIndex].name = v;
        }
    }
}
